<?php

use Recetas\Traits\UtilTrait;
use Recetas\Traits\ValidationTrait;

class RecipesComments extends Eloquent{
	
	use UtilTrait, ValidationTrait;
	
	const RECORDS_PER_PAGE = 10;
	const RECORDS_PER_PAGE_ANSWER = 3;
	
	protected $table = 'recipes_comments';
	
	protected $validationRules = array(
		'comment' 		=> 'required|max:255|min:10',
		'recipe_id' 	=> 'required',
	);
		
	/**
	 * Relations
	 */
	public function user(){
		return $this->belongsTo( 'User' );
	}
	
	public function answersCount(){
		return $this->hasMany( 'RecipesComments', 'parent_comment', 'id' )
			->select( array( DB::raw( 'count(*) as count' ), 'parent_comment' ) )->groupBy( 'parent_comment' );
	}

	public function answersPagination(){
		return $this->hasMany( 'RecipesComments', 'parent_comment', 'id' )->orderBy( 'created_at', 'desc' )
			->take( self::RECORDS_PER_PAGE_ANSWER );
	}
	
	public function likesDislikes(){
		return $this->hasMany( 'CommentsLikesDislikes', 'comment_id' );
	}
	
	/**
	 * Util
	 */
	public function isLike(){
		return $this->like == '1' ? true : false;
	}
	
	public function isDislike(){
		return $this->like == '0' ? true : false;
	}
}
